package jmind.asteroidea.proxy;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import org.springframework.context.ApplicationContext;

import java.lang.reflect.Method;

/**
 * description:
 *
 * @author weibo.xie
 * @date:2022/7/14 下午4:46
 */
@Slf4j
public class CglibAbstractAsteroideaIntefaceProxy extends AbstractAsteroideaInterfaceProxy implements MethodInterceptor {

    public CglibAbstractAsteroideaIntefaceProxy(Class asteroideaInterfaceClass, ApplicationContext applicationContext) {
        super(asteroideaInterfaceClass, applicationContext);
    }

    private Enhancer enhancer = new Enhancer();
    @Override
    public Object getProxy() {
        enhancer.setSuperclass(asteroideaInterfaceClass);
        enhancer.setCallback(this);
        // 返回代理对象，返回的对象起始就是一个封装了"实体类"的代理类，是实现类的实例。
        return enhancer.create();
    }


    @Override
    public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        return super.invoke(proxy,method,args);
    }
}
